<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/2
 * Time: 13:55
 */

namespace app\admin\model;
use think\Model;

class  Discount extends BaseModel
{
    // 定义数据表名
    protected $name = 'discount';

    // 定义主键
    protected $primaryKey = 'id';

    // 定义创建时间字段名
    protected $createTime = 'create_time';

    // 定义更新时间字段名
    protected $updateTime = 'update_time';
    // 定义自动时间戳
    protected $autoWriteTimestamp = false;

    // 设置字段信息
    protected $schema = [
        'id' => 'int',
        'discount_category' => 'int',
        'discount_parent_category' => 'int',
        'method' => 'int',
        'code_title' => 'string',
        'discount_type' => 'int',
        'discount_value' => 'int',
        'applies_to_type' => 'int',
        'applies_to_value' => 'string',
        'min_purchase_req_type' => 'int',
        'min_purchase_req_value' => 'int',
        'eligibility_type' => 'int',
        'eligibility_value' => 'string',
        'max_discount_uses_type' => 'string',
        'max_discount_uses_value' => 'int',
        'combinations' => 'string',
        'activedate_start' => 'datetime',
        'activedate_end' => 'datetime',
        'status' => 'int',
        'source' => 'int',
        'create_time' => 'datetime',
        'creator' => 'int',
        'update_time' => 'datetime',
        'updater' => 'int',
        'is_del' => 'int',
    ];
    protected $attributes = [
        'discount_category'=>[
            1=>'Amount off products',
            2=>'Amount off order',
        ],
        'discount_type'=>[
            1=>'Percentage',
            2=>'Fixed amount',
        ],
        'applies_to_type'=>[
            1=>'Specific Merge Products',
            2=>'Specific Sku Products',
        ],
        'eligibility_type'=>[
            1=>'All Customers',
            3=>'Specific customers',
        ],
        'max_discount_uses_type'=>[
            1=>'Limit number of times this discount can be used in total',
            2=>'Limit to one use per customer',
        ],
        'status'=>[
            0=>'禁用',
            1=>'启用',
        ],
        'min_purchase_req_type'=>[
            1=>'No minimum requirements',
            2=>'Minimum purchase amount ($)',
            3=>'Minimum quantity of items',
        ],
    ];
    #折扣类别  和折扣父类对应关系
    const PARENT_CATEGOTY = [
        1 =>1,
        2 =>2,
    ];
    public function getAttr($name)
    {
        // 检查是否是要处理的特殊属性
        if (strpos($name, '_value') !== false) {
            $originalField = str_replace('_value', '', $name);
            if (isset($this->attributes[$originalField])) {
                $value = $this->getData($originalField);
                return $this->attributes[$originalField][$value] ?? '未知';
            }
        }

        // 如果不是特殊属性，调用父类的方法
        return parent::getAttr($name);
    }
    /**
     * @describe
     * @author: Bobo
     * @date: 2025-02-11 17:05:24
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum($field = 'all')
    {
        return $this->getEnum(strtoupper($field), [], 'label');
    }
}